package cn.tedu.flower.manage.content.controller;


import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.web.JsonResult;
import cn.tedu.flower.manage.content.pojo.param.AddressAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.AddressUpdateParam;
import cn.tedu.flower.manage.content.pojo.vo.AddressListItemVO;
import cn.tedu.flower.manage.content.service.IAddressService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 处理标签相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/content/address")
@Api(tags = "9.9用户查询地址")
@Validated
public class AddressController {

    @Autowired
    private IAddressService iAddressService;

    public AddressController() {
        log.info("创建控制器对象：TagController");
    }

    // 添加@RequestBody：接收对象参数
    // 不添加@RequestBody：接收FormData参数
    @ApiOperation("新增地址")
    @ApiOperationSupport(order = 100)
    @PostMapping("/address/add-new")
    @PreAuthorize("hasAuthority('/content/address/add-new')")
    public JsonResult addNew(@Validated AddressAddNewParam addressAddNewParam) {
        log.debug("开始处理【新增地址】的请求，参数：{}", addressAddNewParam);
        iAddressService.addNew(addressAddNewParam);
        return JsonResult.ok();
    }


    @ApiOperation("删除地址")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标签ID", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/content/address/delete')")
    public JsonResult delete(@PathVariable @Range(min = 1, message = "删除地址失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【删除地址】的请求，参数：{}", id);
        iAddressService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改地址")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update/info")
    @PreAuthorize("hasAuthority('/content/address/update')")
    public JsonResult updateInfoById(@Validated AddressUpdateParam addressUpdateParam) {
        log.debug("开始处理【修改标签】的请求，参数：{}", addressUpdateParam);
        iAddressService.updateById(addressUpdateParam);
        return JsonResult.ok();
    }


    @ApiOperation("启用地址")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标签ID", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/upisdefault")
    @PreAuthorize("hasAuthority('/content/tag/update')")
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "启用地址失败，请提交合法的ID值！") Long id,Integer isDefault) {
        log.debug("开始处理【启用地址】的请求，参数：{}", id);
        iAddressService.updateUpDefaultById(id,isDefault);
        return JsonResult.ok();
    }


    @ApiOperation("禁用地址")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标签ID", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/downdisable")
    @PreAuthorize("hasAuthority('/content/tag/update')")
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "禁用地址失败，请提交合法的ID值！") Long id,Integer isDefault) {
        log.debug("开始处理【禁用地址】的请求，参数：{}", id);
        iAddressService.updateUpDefaultById(id,isDefault);
        return JsonResult.ok();
    }


    @ApiOperation("根据ID查询地址")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标签ID", required = true, dataType = "long")
    })
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/content/address/read')")
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "获取标签详情失败，请提交合法的姓名！") String username) {
        log.debug("开始处理【根据usern查询地址】的请求，参数：{}", username);
        AddressListItemVO addressListItemVO = iAddressService.getAddressByUsername(username);
        return JsonResult.ok(addressListItemVO);
    }

    @ApiOperation("查询所有地址")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
        @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @GetMapping("/type/list")
    @PreAuthorize("hasAuthority('/content/address/read')")
    public JsonResult listTagType(Integer pageNum, Integer pageSize) {

        PageData<AddressListItemVO> pageData =iAddressService.getAddressAll(pageNum,pageSize);
        return JsonResult.ok(pageData);
    }



}